<script>
	export default {
		data() {
			return {

			};
		},
		onLaunch: function() {
			let _this = this;

			//保持屏幕亮着11
			wx.setKeepScreenOn({
				keepScreenOn: true
			})
			
			setTimeout(()=>{
				// 为了解决第一次进入系统在storage中不存在司机状态的问题
				_this.initWorkStatus();
				_this.changeDriverLocation2Geo();
			}, 2000)

		},
		methods: {
			// 初始化司机状态
			initWorkStatus(){
				let _this = this;
				if(!uni.getStorageSync("workStatus")){
					uni.setStorageSync("workStatus", _this.Consts.WORK_STATUS.STOP_ACCPET_ORDER)
				}
			},
			// 监听司机位置变化，并缓存到Redis-GEO中
			changeDriverLocation2Geo() {
				const _this = this;
				// 1.监听司机位置变化触发
				uni.startLocationUpdate();
				uni.onLocationChange(function(res) {
					console.log('App页面监听纬度：' + res.latitude);
					console.log('App页面监听经度：' + res.longitude);
					console.log(uni.getStorageSync("workStatus"));
					if (_this.Consts.WORK_STATUS.START_ACCPET_ORDER == uni.getStorageSync("workStatus")) {
						// 2.把司机当前位置传递到后端保存到GEO中
						let param = {
							"longitude": res.longitude,
							"latitude": res.latitude
						}
						_this.post("/driver/app/driver/locationToGeo", param, (res) => {
							let {
								success,
								message
							} = res;
							if (success) {
								console.log("司机最新位置更新到GEO成功...");
							} else {
								uni.showToast({
									icon: "error",
									title: "位置更新失败，会影响接单，请联系管理员！"
								})
							}
						})
					}
				})
			}
		},
		onShow: function() {
			const _this = this;
		},
		onHide: function() {}
	};
</script>

<style lang="scss">
	@import 'uview-ui/index.scss';
</style>